package com.xtaller.lazy.anync;

import com.xtaller.lazy.lib.convert.J;
import com.xtaller.lazy.lib.convert.V;
import com.xtaller.lazy.lib.kit.JwtUtil;
import com.xtaller.lazy.model.system.ExLog;
import com.xtaller.lazy.service.system.ExLogService;
import io.jsonwebtoken.Claims;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @version V1.0
 * @author: Taller
 * @date: 2019-04-22
 * @Description: 数据异常异步处理
 */
@Component
public class ExTask {
    @Autowired private ExLogService exLogService;

    @Async("taskExecutor")
    public void createEx(ExLog log){
        var token = log.getToken();
        var jwt = JwtUtil.validateJWT(token);
        var uid = 0L;
        if (jwt.isSuccess()) {
            try {
                Claims claims = jwt.getClaims();
                String subject = claims.getSubject();
                if (!V.isEmpty(subject)) {
                    uid = Long.parseLong(claims.getId());
                }
            } catch (Exception ex) {

            }
        }
        log.setCreateId(uid);
        exLogService.save(log);
    }
}
